package smedi.ismedi.web.bff.controller.organization;

import com.smedi.ismedi.organization.api.dto.request.ProjectOrganizationRequest;
import com.smedi.ismedi.organization.api.dto.request.TwoProjectOrganizationNodeRequest;
import com.smedi.ismedi.organization.api.dto.response.ProjectOrganizationResponse;
import com.smedi.ismedi.organization.api.feign.RemoteProjectOrganizationFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.web.bff.controller.BaseController;
import smedi.ismedi.web.bff.dto.ProjectOrganizationPickerResponse;
import smedi.ismedi.web.bff.infrastructure.constant.CustomError;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目组织
 *
 * @author xulinjun
 */
@Slf4j
@RestController
@Api(tags = {"项目组织架构维护接口"})
@RequestMapping("/webbff/v1/projectOrganization")
@AllArgsConstructor
public class ProjectOrganizationController extends BaseController {
    private final RemoteProjectOrganizationFeignApi remoteProjectOrganizationFeignApi;

    /**
     * 根据项目组织id数组获取项目下的二级项目组织值列表
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据项目组织id数组获取项目下的二级项目组织值列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/getProjectOrganizationPickerByProjectId")
    public ResponseData<List<ProjectOrganizationPickerResponse>> getProjectOrganizationPickerByProjectId(@RequestParam(value = "projectId") Long projectId) {
        ResponseData<List<ProjectOrganizationResponse>> responseData = remoteProjectOrganizationFeignApi.getProjectOrganizationTreeRootIsProjectOrganization(projectId);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }

        List<ProjectOrganizationResponse> projectOrganizations = responseData.getData();
        List<ProjectOrganizationPickerResponse> pickers = new ArrayList<>();

        projectOrganizations.stream().forEach(i -> {
            pickers.add(new ProjectOrganizationPickerResponse()
                    .setProjectOrganizationId(i.getProjectOrganizationId())
                    .setProjectOrganizationName(i.getFullName())
                    .setProjectOrganizationShortName(i.getShortName()));
        });

        return new ResponseData<List<ProjectOrganizationPickerResponse>>().success(pickers);
    }

    /**
     * 保存项目组织信息
     *
     * @param projectOrganizationRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "保存组织信息")
    @PostMapping(value = "/save")
    public ResponseData<ProjectOrganizationResponse> postSaveProjectOrganization(@RequestBody ProjectOrganizationRequest projectOrganizationRequest) {
        return remoteProjectOrganizationFeignApi.postSaveProjectOrganization(projectOrganizationRequest);
    }

    /**
     * 删除项目组织信息
     *
     * @param projectOrganizationId
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除组织信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "项目组织id", dataType = "Long", dataTypeClass = Long.class),
    })
    @DeleteMapping(value = "/delete")
    public ResponseData<Boolean> deleteProjectOrganization(@RequestParam("projectOrganizationId") Long projectOrganizationId) {
        return remoteProjectOrganizationFeignApi.deleteProjectOrganization(projectOrganizationId);
    }

    /**
     * 查询项目组织树，仅显示组织
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询项目组织树，仅显示组织")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/getProjectOrganizationTreeRootIsProjectOrganization")
    public ResponseData<List<ProjectOrganizationResponse>> getProjectOrganizationTreeRootIsProjectOrganization(@RequestParam(value = "projectId") Long projectId) {
        return remoteProjectOrganizationFeignApi.getProjectOrganizationTreeRootIsProjectOrganization(projectId);
    }

    /**
     * 查询项目组织树，项目作为顶点组织
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询项目组织树，项目作为顶点组织")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/getProjectOrganizationTreeRootIsProject")
    public ResponseData<ProjectOrganizationResponse> getProjectOrganizationTreeRootIsProject(@RequestParam(value = "projectId") Long projectId) {
        return remoteProjectOrganizationFeignApi.getProjectOrganizationTreeRootIsProject(projectId);
    }

    /**
     * 查询项目组织视图
     *
     * @param projectOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询组织视图")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "项目组织id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/getProjectOrganizationView")
    public ResponseData<ProjectOrganizationResponse> getProjectOrganizationView(@RequestParam(value = "projectOrganizationId") Long projectOrganizationId) {
        return remoteProjectOrganizationFeignApi.getProjectOrganizationView(projectOrganizationId);
    }

    /**
     * 判断当前项目组织是否可以用
     *
     * @param projectOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "判断当前项目组织是否可以用")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "项目组织id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/checkAvailable")
    public ResponseData<Boolean> getCheckAvailable(@RequestParam(value = "projectOrganizationId") Long projectOrganizationId) {
        return remoteProjectOrganizationFeignApi.getCheckAvailable(projectOrganizationId);
    }

    /**
     * 获取当前组织下的所有子节点组织id及其本身
     *
     * @param projectOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取当前组织下的所有子节点组织id及其本身")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "项目组织id", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/getProjectOrganizationChildrenProjectOrganizationIdList")
    public ResponseData<List<Long>> getProjectOrganizationChildrenProjectOrganizationIdList(@RequestParam(value = "projectOrganizationId") Long projectOrganizationId) {
        return remoteProjectOrganizationFeignApi.getProjectOrganizationChildrenProjectOrganizationIdList(projectOrganizationId);
    }

    /**
     * 获取当前组织下的所有子节点组织id与组织名称及其本身
     *
     * @param projectOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取当前组织下的所有子节点组织id与组织名称及其本身")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "组织id", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/getProjectOrganizationChildrenMap")
    public ResponseData<Map<Long, String>> getProjectOrganizationChildrenMap(@RequestParam(value = "projectOrganizationId") Long projectOrganizationId) {
        return remoteProjectOrganizationFeignApi.getProjectOrganizationChildrenMap(projectOrganizationId);
    }

    /**
     * 校验当前项目组织可以成为配置项的组织
     *
     * @param projectOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "校验当前项目组织可以成为配置项的组织")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "组织id", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/checkProjectOrganizationCanBeConfigOrganization")
    public ResponseData<Boolean> getCheckProjectOrganizationCanBeConfigOrganization(@RequestParam(value = "projectOrganizationId") Long projectOrganizationId) {
        return remoteProjectOrganizationFeignApi.getCheckProjectOrganizationCanBeConfigOrganization(projectOrganizationId);
    }

    /**
     * 检查项目组织是否为外部单位
     *
     * @param projectOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "检查项目组织是否为外部单位")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/checkProjectOrganizationIsExternalUnit")
    public ResponseData<Boolean> getCheckProjectOrganizationIsExternalUnit(@RequestParam(value = "projectOrganizationId") Long projectOrganizationId) {
        return remoteProjectOrganizationFeignApi.getCheckProjectOrganizationCanBeConfigOrganization(projectOrganizationId);
    }

    /**
     * 根据项目组织id数组获取项目组织信息
     *
     * @param projectOrganizationIds
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据项目组织id数组获取项目组织信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectOrganizationIds", value = "项目组织id数组", dataType = "List", dataTypeClass = List.class),
    })
    @GetMapping(value = "/queryProjectOrganizationList")
    public ResponseData<List<ProjectOrganizationResponse>> getQueryProjectOrganizationList(@RequestParam(value = "projectOrganizationIds") List<Long> projectOrganizationIds) {
        return remoteProjectOrganizationFeignApi.getQueryProjectOrganizationList(projectOrganizationIds);
    }

    /**
     * 根据组织名称模糊查询项目组织列表
     *
     * @param fullName
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据组织名称模糊查询项目组织列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "fullName", value = "项目组织名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long"),
    })
    @GetMapping(value = "/queryProjectOrganizationListByFullName")
    public ResponseData<List<ProjectOrganizationResponse>> getQueryProjectOrganizationListByFullName(@RequestParam(value = "fullName") String fullName, @RequestParam(value = "projectId", required = false) Long projectId) {
        return remoteProjectOrganizationFeignApi.getQueryProjectOrganizationListByFullName(fullName, projectId);
    }

    /**
     * 根据项目id获取项目下的企业组织列表
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据项目id获取项目下的企业组织列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryProjectOrganizationListByProjectId")
    public ResponseData<List<ProjectOrganizationResponse>> getQueryProjectOrganizationListByProjectId(@RequestParam(value = "projectId") Long projectId) {
        return remoteProjectOrganizationFeignApi.getQueryProjectOrganizationListByProjectId(projectId);
    }

    /**
     * 根据项目id和特殊类型获取到符合的项目组织
     *
     * @param projectId
     * @param specialTypes
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据项目id和特殊类型获取到符合的项目组织")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "specialTypes", value = "特殊类型", dataType = "List", dataTypeClass = List.class)
    })
    @GetMapping(value = "/getProjectOrganizationByProjectIdAndSpecialType")
    public ResponseData<List<ProjectOrganizationResponse>> getProjectOrganizationByProjectIdAndSpecialType(@RequestParam("projectId") Long projectId,
                                                                                                           @RequestParam("specialTypes") List<String> specialTypes) {
        return remoteProjectOrganizationFeignApi.getProjectOrganizationByProjectIdAndSpecialType(projectId, specialTypes);
    }

    /**
     * 交换两个项目组织的排序，并获取项目组织树
     *
     * @param twoProjectOrganizationNodeRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "交换两个项目组织的排序，并获取项目组织树")
    @PostMapping(value = "/exchangeTwoProjectOrganizationNodeSort")
    public ResponseData<List<ProjectOrganizationResponse>> postExchangeTwoProjectOrganizationNodeSort(@RequestBody TwoProjectOrganizationNodeRequest twoProjectOrganizationNodeRequest) {
        return remoteProjectOrganizationFeignApi.postExchangeTwoProjectOrganizationNodeSort(twoProjectOrganizationNodeRequest);
    }
    /**
     * 根据项目id获取项目下的分包单位
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据项目id获取项目下的分包单位")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryProjectOrganizationsByProjectId")
    ResponseData<List<String>> getQueryProjectOrganizationsByProjectId(@RequestParam(value = "projectId") Long projectId){
        return remoteProjectOrganizationFeignApi.getQueryProjectOrganizationsByProjectId(projectId);
    }
}
